# -*- coding: utf-8 -*-


import struct
import numpy as np


def read_pkg_file(pkg_file_path):
    """
    以二进制格式读取特征文本文件
    """
    file = open(pkg_file_path, "rb")
    l_id = file.read(4)
    l_id = int.from_bytes(l_id, byteorder='little')
    uodr_woker_id = file.read(1)
    uodr_woker_id = struct.unpack('<b', uodr_woker_id)[0]
    uodr_begin_time = file.read(4)
    uodr_begin_time = int.from_bytes(uodr_begin_time, byteorder='little')
    uodr_end_time = file.read(4)
    uodr_end_time = int.from_bytes(uodr_end_time, byteorder='little')
    uodr_report_time = file.read(4)
    uodr_report_time = int.from_bytes(uodr_report_time, byteorder='little')
    uodr_report_index = file.read(2)
    uodr_report_index = int.from_bytes(uodr_report_index, byteorder='little')
    uodr_up_or_down = file.read(1)
    uodr_up_or_down = struct.unpack('<b', uodr_up_or_down)[0]
    # uodr_up_or_down = int.from_bytes(uodr_up_or_down, byteorder='little')
    uodr_pkgs_size = file.read(4)
    uodr_pkgs_size = int.from_bytes(uodr_pkgs_size, byteorder='little')
    uodr_pkg = np.frombuffer(file.read(), dtype=np.float32).tolist()
    file.close()
    result = {
        "l_id": l_id,
        "uodr_woker_id": uodr_woker_id,
        "uodr_begin_time": uodr_begin_time,
        "uodr_end_time": uodr_end_time,
        "uodr_report_time": uodr_report_time,
        "uodr_report_index": uodr_report_index,
        "uodr_up_or_down": uodr_up_or_down,
        "uodr_pkgs_size": uodr_pkgs_size,
        "uodr_pkg": uodr_pkg
    }
    return result
